import { fileURLToPath, URL } from 'node:url'

import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'

import { createHtmlPlugin } from "vite-plugin-html";

import removeConsole from "vite-plugin-remove-console";   // 线上环境删除console


// https://vitejs.dev/config/
export default ({mode}) => {
  const env = loadEnv(mode, process.cwd())         // 获取环境变量
  return defineConfig({
    plugins: [
      vue(),
      removeConsole(),
      createHtmlPlugin({
        inject: {
          data: {
            //方便 index.html页面使用 BASE_TITLE 获取系统标题
            BASE_TITLE: env?.VITE_VUE_BASE_TITLE,
          },
        },
      })
    ],
    resolve: {
      alias: {   // 别名
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    },
    base: './',                     // 基础路径，相对路径 ./ 和绝对路径的配置  /
    server: {
      host: "0.0.0.0",    // 开启本地IP访问
      hmr: true
    },
    build: {
      chunkSizeWarningLimit: 500,
      rollupOptions: {
        experimentalLogSideEffects: true,
        output: {
          experimentalMinChunkSize: 20 * 1024,
          manualChunks: (id) => {
            return id.toString().split('node_modules/')[1].split('/')[0].toString();
          }
        }
      }
    },
    css: {
      // css预处理器sass
      preprocessorOptions: {
        scss: {
          additionalData: `@use "@/assets/css/variable.scss" as *; @use "@/assets/css/mixin.scss" as *;`    // 使用@use代替@import ，@use只能在dart-sass中使用
        }
      },
    }
  })
}
